package org.esgi.xerces;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;


public class Parser {
	
	private String _url = "/Users/fabrice/Sites/xmlparser/application/models/DB/";
	public static int maxlength = 0;
	
	public Parser(){
	}
	
	public void Init() throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(this._url+"facture.xml");
		
		Node n = doc.getDocumentElement();
		
		System.out.println(this._url+"facture.xml");
		System.out.println(doc.getXmlVersion());
		
	}
	
	public String Affichage(){
		return null;
	}
	
	@SuppressWarnings("unused")
	private Node _readDoc(String file) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(file);
		
		Node n = doc.getDocumentElement();
		
		return n;
	}
	
	public void countNodeType(String file) throws ParserConfigurationException, SAXException, IOException{
		file = this._url.concat(file);
		System.out.println(file);
	}
	
	public NodeList listFacture() throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		//dbf.setValidating(true); // Validation
		dbf.setIgnoringElementContentWhitespace(true); // Ignoring whitespace
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(this._url+"facture.xml");
		
		
		Element racine = doc.getDocumentElement();
		//System.out.println(racine.getNodeName());
		String tag = "facture";
		NodeList factures = racine.getElementsByTagName(tag);
		
		//print(doc);
//		System.out.println(factures.getLength());
//		for (int i = 0; i < factures.getLength(); i++) {
//			System.out.println(factures.item(i).getNodeName());
//			
//			NodeList facture = factures.item(i).getChildNodes();
//			
//			for (int j = 0; j < facture.getLength(); j++) {
//				System.out.println(facture.item(j).getNodeName());
//			}
//			
//		}
		return factures;
	}
	
	public void domParser() {
		DOMParser parser = new DOMParser();
		String validation = "http://xml.org/sax/features/validation";
		String whitespace = "http://apache.org/xml/features/dom/include-ignorable-whitespace";
		
		try {
			parser.setFeature(validation, true);
			parser.setFeature(whitespace, false);
			parser.parse(this._url+"facture.xml");
			Document doc = parser.getDocument();
			Node root = doc.getDocumentElement();
			print(doc);
			System.out.println("Commentaire le plus long : "+maxlength);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void print (Node node) {
		if((node.getNodeType() == 3)){
			System.out.println(node.getNodeName() + ":" + node.getNodeValue()+ " " +node.getParentNode().getNodeName());
			if(maxlength < (node.getNodeValue().length()))
				maxlength = node.getNodeValue().length();
		}
		if(node.hasChildNodes()){
			NodeList nl = node.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				print(nl.item(i));
			}
		}
	}
	
}

